﻿package communication1.pfl;
import communication1.pfl.*;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RecordActivity extends Activity {
	  public static final int PHOTOHRAPH = 4;// 拍照  
	    public static final int PHOTOHRAPH1 = 9;
	   int biaozhi=0;//总开关
	    int flagt=0;//文字发送标识
	    int flagp=0;//图片发送标识
	    int flagv=0;//视频发送标识
	    int flagp2=0;//图片发送标识
	    int flagv2=0;//视频发送标识
	    String pic_name="";
		String video_name="";
		
		String pic_name2="";
		String video_name2="";
	    int count111=0;
	    String zhaopianlujing;
	private static final int DIALOG = 99;
	private static final int DIALOG1 = 100;
	private static final int DIALOG10 =101 ;
	private static final int DIALOG11 =102 ;
	private static final int DIALOG12 =103 ;
	private static final int DIALOG13 =104 ;
	private static final int DIALOG14 =105 ;
	private static final int DIALOG15 =106 ;
	private static final int DIALOG16 =107 ;
	private static final int DIALOG17 =108 ;
	   String shipinlujing;  
	private EditText rain_num, delta, water_furture, emergency_rate 
			;
	private TextView rain_num_choose = null, delta_choose,capture,record_video,
			water_furture_choose, emergency_rate_choose;

	private Button pre, next = null;
	private String[] array_rain_num = null,array_rain_delta=null,array_water_furture,array_emergency_rate;
	
	
	private Uri photoUri;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.record_v2);

		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		{
			array_rain_num = getResources().getStringArray(
					R.array.array_rain_num);
			array_rain_delta = getResources().getStringArray(R.array.array_rain_delta);
			array_water_furture =getResources().getStringArray(R.array.array_water_furture);
			array_emergency_rate  = getResources().getStringArray(R.array.array_emergency_rate);
		}

		{

			rain_num = (EditText) findViewById(R.id.rain_num);
			findViewById(R.id.next).setOnClickListener(
					new View.OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							
							Toast.makeText(RecordActivity.this,
									rain_num.getText().toString()+delta.getText().toString(),
									Toast.LENGTH_SHORT).show();
							jump_remark();
						}
					});

			findViewById(R.id.rain_num_choose).setOnClickListener(
					new OnClickListener() {

						@Override
						public void onClick(View v) {
							AlertDialog.Builder alertDialog = new AlertDialog.Builder(
									RecordActivity.this);
							alertDialog.setTitle("请选择雨量"); // 设置标题
							alertDialog.setItems(
									
									array_rain_num,
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {

											rain_num.setText(array_rain_num[which]);
										}
									});
							alertDialog.show();// 显示出来
						}
					});
		}

		{
			delta = (EditText) findViewById(R.id.delta);
			findViewById(R.id.delta_choose).setOnClickListener(
					new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							new AlertDialog.Builder(RecordActivity.this).setTitle("请选择封宽").setItems(array_rain_delta, new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface arg0, int arg1) {
									// TODO Auto-generated method stub
									delta.setText(array_rain_delta[arg1]);
								}
							}).create().show();
						}
					});
		}
		
		{

			water_furture = (EditText) findViewById(R.id.water_furture);
			findViewById(R.id.water_furture_choose).setOnClickListener(
					new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							new AlertDialog.Builder(RecordActivity.this).setTitle("请选择地下水动态").setItems(array_water_furture, new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface arg0, int arg1) {
									// TODO Auto-generated method stub
									water_furture.setText(array_water_furture[arg1]);
								}
							}).create().show();
						}
					});
		
		}
		
		{


			emergency_rate = (EditText) findViewById(R.id.emergency_rate);
			findViewById(R.id.emergency_rate_choose).setOnClickListener(
					new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							new AlertDialog.Builder(RecordActivity.this).setTitle("请选择危险等级").setItems(array_emergency_rate, new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface arg0, int arg1) {
									// TODO Auto-generated method stub
									emergency_rate.setText(array_emergency_rate[arg1]);
								}
							}).create().show();
						}
					});
		
		
		}
		
		{
			capture = (TextView) findViewById(R.id.capture);capture.setOnClickListener(new OnClickListener() {
				
				

				

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					capture();
				}

				
			});
			record_video=(TextView) findViewById(R.id.record_video);record_video.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					record_video();
				}
			});
		
			findViewById(R.id.pre).setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					finish();
				}
			});
		}
		
	}
	
	private void capture() {
		Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                            
        ContentValues values = new ContentValues();  //创建一个内容提供者           
        photoUri = RecordActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  
        imageCaptureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);                          
		startActivityForResult(imageCaptureIntent, PHOTOHRAPH);
	}
	public void jump_remark() {
		Intent intent = new Intent(this, SendActivity.class);
		Bundle b = new Bundle();  
        b.putCharSequence( "rain", rain_num.getText().toString() );  
        b.putCharSequence( "fengkuan", delta.getText().toString() );  
        b.putCharSequence( "undergraund", water_furture.getText().toString() );  
        b.putCharSequence( "safe", emergency_rate.getText().toString() ); 
        b.putCharSequence( "pic_name", pic_name ); 
        b.putCharSequence( "zhaopianlujing", zhaopianlujing ); 
        b.putCharSequence( "video_name", video_name ); 
        b.putCharSequence( "shipinlujing", shipinlujing ); 
        b.putCharSequence( "flagp", flagp+"" ); 
        b.putCharSequence( "flagv", flagp+"" ); 
        
        
        intent.putExtras( b );  
		startActivityForResult(intent, 21);
	}

	public void record_video(){
		Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);        	
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);        		
		startActivityForResult(intent, PHOTOHRAPH1);   
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
//		switch (resultCode) {
//		case RESULT_OK:
//			finish();
//			break;
//
//		default:
//			super.onActivityResult(requestCode, resultCode, data);
//			break;
//		}
		
		switch (requestCode) {
		case PHOTOHRAPH:
			 
       	 if (resultCode == RESULT_OK) 
       	 { 
     		 
            flagp=1;
            flagp2=1;
            Log.v("pfl","准备获取数据:"+data);
           
            ContentResolver cr = this.getContentResolver();  
            Cursor cursor = cr.query(photoUri, null, null, null, null);  
            cursor.moveToFirst(); //游标移到第一条就是系统默认的路径
            if (cursor != null) {  
            zhaopianlujing = cursor.getString(1);  //获取到的路径可以用于图片自定义压缩了
                cursor.close(); 
                Log.v("pfl","照片路径是："+zhaopianlujing); 
            }
           
            pic_name = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";//照片命名
 
            try
            {
            	capture.setText("已拍照");
            EditText txt=(EditText)findViewById(R.id.edit10);
    		 txt.setText("已拍照"); 
            }
            catch(Exception e)
            {
           	 
            }
    		 try
    		 {
    		 EditText txt1=(EditText)findViewById(R.id.pic2_text);
//    		 record_video.setText("已拍照"); 
    		 }
    		 catch(Exception e)
    		 {
    			 
    		 }
       	 }      	             	       	
       
			break;
		case PHOTOHRAPH1:
			  
        	if (resultCode == RESULT_OK)
        	{        
        		     flagv=1;
        		     flagv2=1;
        		     Uri uriVideo = data.getData();        		
        		     Cursor cursor=this.getContentResolver().query(uriVideo, null, null, null, null);        		
        		     if (cursor.moveToNext()) //实际只查询了一次
        		     {        		        		       
        		     shipinlujing = cursor.getString(cursor.getColumnIndex("_data"));        	      		     
        		     video_name = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".3gp";  
        		     } 
        		     
        		     try
        		     {
        		     EditText txt=(EditText)findViewById(R.id.edit11);
//             		 txt.setText("已摄像");
        		     record_video.setText("已摄像"); 
        		     }
        		     catch(Exception e)
        		     {
        		    	 
        		     }
             		 try
             		 {
             		 EditText txt1=(EditText)findViewById(R.id.vid2_text);
            		 txt1.setText("已摄像"); 
             		 }
             		 catch(Exception e)
             		 {
             			 
             		 }
        		           		
        	}
        	break;
		case 21:
			if(resultCode==RESULT_OK){
				finish();
			}
			break;
		default:
			break;
		}
		
	}

}
